<!DOCTYPE html>
<html lang="zh" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <title>Docker Compose 部署Halo教程 - Ryan Wang's Blog</title>
    <meta charset="utf-8">
    <meta name="X-UA-Compatible" content="IE=edge">
    <meta name="author" content="Ryan Wang">
    <meta name="description" content="上一篇文章讲到了使用Docker运行Halo，但是仅仅只是运行起来了Halo，并没有讲到如何使用Nginx进行反向代理以绑定域名，也没有讲到如何配置HTTPS，所以这篇文章将向大家演示一下如何使用DockerCompose部署Halo，并自动配置Nginx以及SSL证书。整个过程最慢不超过10分钟。">
    <meta name="keywords" content="Docker Compose 部署Halo教程,ryan0up,halo博客,halo,java,ruibaby,ryanwang,">
    <link rel="alternate" type="application/atom+xml" title="ATOM 1.0" href="/atom.xml">
    <meta charset="utf-8">
    <meta name="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="telephone=no" name="format-detection">
    <meta name="renderer" content="webkit">
    <meta name="theme-color" content="#ffffff">
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css" integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE=" crossorigin="anonymous">
    <link rel="stylesheet" href="/halo-theme-Journal/source/css/journal.min.css">
        <meta name="robots" content="none">
    <meta name="generator" content="Halo "/>
    <script data-ad-client="ca-pub-5271828906478846" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

        <link rel="shortcut icon" type="images/x-icon" href="https://ryanc.cc/upload/2018/4/favicon.png">
    <script src="/halo-theme-Journal/source/js/loadCSS.js"></script>

    <!-- Import prettify css  -->
            <link rel="stylesheet" href="/halo-theme-Journal/source/css/prettify.min.css">
            <link rel="stylesheet" href="/halo-theme-Journal/source/css/prettify/github-v2.min.css">

    <script>
        loadCSS("https://fonts.googleapis.com/css?family=Lora|Montserrat|Fira+Mono|Material+Icons");
    </script>
    <!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-110780416-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-110780416-1');
</script>

    <noscript>
        <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lora|Montserrat|Anonymous+Pro:400|Material+Icons"/>
    </noscript>
</head>
<body>
<div id="top"></div>
<div id="app"><div class="single-column-drawer-container" ref="drawer"
     v-bind:class="{ 'single-column-drawer-container-active': isDrawerOpen }">
    <div class="drawer-content">
        <div class="drawer-menu">
                            <a class="a-block drawer-menu-item" href="/" target="_self">
                    Home
                </a>
                <a class="a-block drawer-menu-item" href="/archives" target="_self">
                    Archives
                </a>
                <a class="a-block drawer-menu-item" href="/links" target="_self">
                    Links
                </a>
                <a class="a-block drawer-menu-item" href="/journals" target="_self">
                    Journals
                </a>
                <a class="a-block drawer-menu-item" href="/s/about" target="_self">
                    About
                </a>

            <a class="a-block drawer-menu-item" href="/atom.xml">
                RSS
            </a>
        </div>
    </div>
</div>
<transition name="fade">
    <div v-bind:class="{ 'single-column-drawer-mask': mounted }" v-if="isDrawerOpen" v-on:click="toggleDrawer"></div>
</transition>
<nav ref="navBar" class="navbar sticky-top navbar-light single-column-nav-container">
    <div ref="navBackground" class="nav-background"></div>
    <div class="container container-narrow nav-content">
        <button id="nav_dropdown_btn" class="nav-dropdown-toggle" type="button" v-on:click="toggleDrawer">
            <i class="material-icons">
                menu
            </i>
        </button>
        <a ref="navTitle" class="navbar-brand" href="">
            Ryan Wang's Blog
        </a>
    </div>
</nav>
<div class="single-column-header-container" ref="pageHead"
     v-bind:style="{ transform: 'translateZ(0px) translateY('+.3*scrollY+'px)', opacity: 1-navOpacity }">
    <a href="">
        <div class="single-column-header-title">Ryan Wang's Blog</div>
        <div class="single-column-header-subtitle">Life is fantastic.</div>
    </a>
</div><div ref="sideContainer" class="side-container">
    <a class="a-block nav-head " href="">
        <div class="nav-title">
            Ryan Wang's Blog
        </div>
        <div class="nav-subtitle">
            Life is fantastic.
        </div>
    </a>

    <div class="nav-link-list">

                <a class="a-block nav-link-item" href="/" target="_self">
                    Home
                </a>
                <a class="a-block nav-link-item" href="/archives" target="_self">
                    Archives
                </a>
                <a class="a-block nav-link-item" href="/links" target="_self">
                    Links
                </a>
                <a class="a-block nav-link-item" href="/journals" target="_self">
                    Journals
                </a>
                <a class="a-block nav-link-item" href="/s/about" target="_self">
                    About
                </a>

        <a class="a-block no-tint nav-link-item" href="/atom.xml">
            RSS
        </a>
    </div>

    <div class="nav-footer">
        Proudly published with <a href="https://halo.run/" target="_blank" rel="noreferrer noopener">Halo</a><br>
        Theme <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> by <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a><br>
        &copy; 2019 <a href="">Ryan Wang's Blog</a>
    <br />
Server on Raspberry Pi
<br />
<a href="https://www.upyun.com/" target="_blank"><img src="/upload/2018/5/又拍云_logo5.png" style="width:48px"></img></a>
<script>
console.log("%c    __  __      __\n" +
                "   / / / /___ _/ /___\n" +
                "  / /_/ / __ `/ / __ \\\n" +
                " / __  / /_/ / / /_/ /\n" +
                "/_/ /_/\\__,_/_/\\____/ %c v1.1.1 https://github.com/halo-dev/halo","color:#4571ca;","color:red");
</script>
    </div>
</div><div ref="extraContainer" class="extra-container">
    <div class="pagination">
        <a id="globalBackToTop" class="pagination-action animated-visibility" href="#top" :class="{ invisible: scrollY == 0 }">
            <i class="material-icons pagination-action-icon">
                keyboard_arrow_up
            </i>
        </a>

    </div>
</div>
<div ref="streamContainer" class="stream-container">
    <div class="post-list-container post-list-container-shadow">
        <div class="post">
            <div class="post-head-wrapper-text-only">
                <div class="post-title">
                    Docker Compose 部署Halo教程
                    <div class="post-meta">
                        <time datetime="2018-12-8 14:31:00" itemprop="datePublished">
                            2018-12-08 14:31
                        </time>&nbsp;
                            <i class="material-icons" style="">folder</i>
                                <a href='/categories/study-notes'>学习笔记</a>

                            <i class="material-icons" style="">label</i>
                                <a href='/tags/halo'>Halo</a>, 
                                <a href='/tags/java'>Java</a>, 
                                <a href='/tags/docker'>Docker</a>
                    </div>
                </div>
            </div>

            <div class="post-body-wrapper">
                <div class="post-body" v-pre>
                    <html>
 <head></head>
 <body>
  <blockquote> 
   <p>上一篇文章讲到了使用 Docker 运行 Halo，但是仅仅只是运行起来了 Halo，并没有讲到如何使用 Nginx进行反向代理以绑定域名，也没有讲到如何配置 HTTPS，所以这篇文章将向大家演示一下如何使用 Docker Compose 部署Halo，并自动配置 Nginx 以及 SSL证书。整个过程最慢不超过10分钟。</p> 
  </blockquote> 
  <h2 id="前言">前言</h2> 
  <p>此教程适用于语文及格的同学，不需要你精通什么杂七杂八的技术，所有配置都已经放在 <code>docker-compose.yaml</code> 里面了（我都配置好了）。你只需要在其中改一下域名，持有者邮箱，数据库用户密码什么的就行了。另外，该 <code>Docker</code> 镜像默认使用的是 <code>H2Database</code> ，用户名和密码是自定义的。所以请不要再问我 MySQL 怎么装？？数据库用户名和密码是什么？？讲真，就算你一点都不会 <code>Docker</code>，你按照这个教程一步一步来，安装失败算我输。</p> 
  <h2 id="安装-docker-和-docker-compose">安装 Docker 和 Docker Compose</h2> 
  <p>这里使用的服务器系统版本为 CentOS 7.3，其他 Linux发行版安装 Docker 和 Docker Compose 的方式大同小异，大家自行查阅资料。</p> 
  <h3 id="安装必要依赖">安装必要依赖</h3> 
  <pre><code class="language-bash">sudo yum install -y yum-utils device-mapper-persistent-data lvm2
</code></pre> 
  <h3 id="添加软件源">添加软件源</h3> 
  <pre><code class="language-bash">sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
</code></pre> 
  <h3 id="更新-yum-缓存">更新 yum 缓存</h3> 
  <pre><code class="language-bash">sudo yum makecache fast
</code></pre> 
  <h3 id="安装-docker">安装 Docker</h3> 
  <pre><code class="language-bash">sudo yum -y install docker-ce
</code></pre> 
  <h3 id="启动-docker">启动 Docker</h3> 
  <pre><code class="language-bash">sudo systemctl start docker
</code></pre> 
  <h3 id="设置-docker-为开机启动">设置 Docker 为开机启动</h3> 
  <pre><code class="language-bash">sudo systemctl enable docker
</code></pre> 
  <h3 id="安装-docker-compose">安装 Docker Compose</h3> 
  <pre><code class="language-bash">sudo yum -y install docker-compose
</code></pre> 
  <h3 id="添加国内镜像源">添加国内镜像源</h3> 
  <p>添加或修改 <code>/etc/docker/daemon.json</code> 这个文件，加入下面的配置。</p> 
  <pre><code class="language-json">{
  "registry-mirrors": ["http://hub-mirror.c.163.com"]
}
</code></pre> 
  <h3 id="重启-docker-使配置生效">重启 Docker 使配置生效</h3> 
  <pre><code class="language-bash">service docker restart
</code></pre> 
  <h2 id="下载-docker-compose-文件">下载 Docker Compose 文件</h2> 
  <p>好了，经过上面一顿操作之后，服务器的 Docker 和 Docker Compose 就已经可以正常使用了。下面我们就可以正式开始部署 Halo 了。首先我们需要下载好 <code>docker-compose.yaml</code> 文件，这个文件在项目目录里面有，我们可以直接使用 <code>wget</code> 命令从 github 上下载下来，如下操作：</p> 
  <pre><code class="language-bash">yum install -y wget &amp;&amp; wget -O docker-compose.yaml https://git.io/fpS8N
</code></pre> 
  <h2 id="修改-docker-compose-文件">修改 Docker Compose 文件</h2> 
  <p>假设你现在已经下载好了 <code>docker-compose.yaml</code> 文件，那么在部署之前需要做的就是简单修改以下这个文件。因为我们需要绑定域名以申请SSL证书，所以你需要在 <code>docker-compose.yaml</code> 文件中修改以下配置：</p> 
  <pre><code class="language-docker">  halo:
    restart: always
    image: ruibaby/halo
    container_name: halo
    ports:
      - 8090:8090
    environment:
      - VIRTUAL_PORT=8090
      - VIRTUAL_HOST=localhost  # 监听的地址（务必修改）
      - LETSENCRYPT_HOST=localhost # 证书的域名 （务必修改）
      - LETSENCRYPT_EMAIL=i@example.com # 证书所有者的邮箱，快过期时会提醒（务必修改）
      - DB_USER=admin # h2数据库用户名，自定义（务必修改）
      - DB_PASSWORD=123456 # h2数据库密码，自定义（务必修改）
    volumes:
      - ~/halo:/root/halo
</code></pre> 
  <p>参考配置：</p> 
  <pre><code class="language-docker">  halo:
    restart: always
    image: ruibaby/halo
    container_name: halo
    ports:
      - 8090:8090
    environment:
      - VIRTUAL_PORT=8090
      - VIRTUAL_HOST=blog.ryanc.cc  # 监听的地址（务必修改）
      - LETSENCRYPT_HOST=blog.ryanc.cc # 证书的域名 （务必修改）
      - LETSENCRYPT_EMAIL=i@ryanc.cc # 证书所有者的邮箱，快过期时会提醒（务必修改）
      - DB_USER=ruibaby # h2数据库用户名，自定义（务必修改）
      - DB_PASSWORD=qwert123 # h2数据库密码，自定义（务必修改）
    volumes:
      - ~/halo:/root/halo
</code></pre> 
  <p>如上配置，我们需要修改 <code>VIRTUAL_HOST</code> , <code>LETSENCRYPT_HOST</code> 为自己的域名，<strong>注意注意注意！</strong>，在部署之前最好先将外网IP解析到域名，因为申请 SSL 证书需要域名可以正常访问。另外，<code>LETSENCRYPT_EMAIL</code> 这个节点是证书申请者的邮箱，当证书要过期的时候，会提醒你续签，<code>DB_USER</code> 和 <code>DB_PASSWORD</code> 分别为 <code>H2Database</code> 的用户名和密码，请务必要修改，这个是自定义的，不要使用默认的，修改完成之后保存文件备用。</p> 
  <h2 id="下载配置文件模板">下载配置文件模板</h2> 
  <p>因为要涉及到 Nginx 反向代理，所以我们需要使用模板生成一个 Nginx 的配置文件，以配置好需要代理的地址以及后面 SSL 证书的配置。这个模板人家已经写好了，所以我们直接下载下来即可，下载下来我们就不需要管了。</p> 
  <pre><code class="language-bash">cd /etc &amp;&amp; mkdir nginx

curl https://raw.githubusercontent.com/jwilder/nginx-proxy/master/nginx.tmpl &gt; /etc/nginx/nginx.tmpl
</code></pre> 
  <h2 id="运行">运行</h2> 
  <p>如果你顺利进行了上面所述步骤，那么已经离成功不远了，现在我们只需要执行一条命令即可完成 Halo 的部署。</p> 
  <pre><code class="language-bash">docker-compose up -d
</code></pre> 
  <h2 id="版本更新">版本更新</h2> 
  <pre><code class="language-bash"># 拉取最新的 Halo 镜像
docker pull ruibaby/halo

# 暂停 Docker 容器运行
docker-compose stop

# 重新构建容器
docker-compose up -d --build
</code></pre> 
  <h2 id="常用命令">常用命令</h2> 
  <pre><code class="language-bash"># 停止容器运行
docker-compose stop

# 启动容器
docker-compose start

# 重启容器
docker-compose restart
</code></pre> 
  <h2 id="注意事项">注意事项</h2> 
  <ol> 
   <li>最好提前解析好IP，绑定域名。</li> 
   <li>运行完成之后可能得等几分钟，HTTPS才会配置好，如果不能访问属正常现象，等待几分钟就可以看到 Halo 安装的界面了。</li> 
   <li>Docker 镜像使用的是 H2 数据库，所以不需要你自己安装，服务启动就可以自动创建了，求求你们别再问我 H2 数据库怎么安装了。</li> 
  </ol> 
  <h2 id="参考文章使用的开源镜像">参考文章/使用的开源镜像</h2> 
  <ul> 
   <li><a href="https://www.cnblogs.com/chenug/p/6916639.html">在容器中利用Nginx-proxy实现多域名的自动反向代理、免费SSL证书</a></li> 
   <li><a href="https://github.com/jwilder/docker-gen">jwilder/docker-gen</a></li> 
   <li><a href="https://github.com/jwilder/nginx-proxy">jwilder/nginx-proxy</a></li> 
  </ul> 
 </body>
</html>
                </div>
            </div>

<nav class="post-pagination">
        <a class="newer-posts" href="/archives/docker-compose-run-jpress">
        上一篇<br>Docker Compose 部署JPress教程
    </a>
    <span class="page-number"></span>
    <a class="older-posts" href="/archives/no-refresh-to-next-page">
        下一篇<br>无刷新加载下一页方案解析
    </a>
</nav>

    <div class="post-comment-wrapper">
      	<script>
        var settings = {
        	autoLoad: false
        }  
        </script>
        <halo-comment id="40" type="post" :settings="settings"/>
    </div>
        </div>
    </div>
<div class="single-column-footer">
    Proudly published with <a href="https://halo.run/" target="_blank" rel="noreferrer noopener">Halo</a><br>
        Theme <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> by <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a><br>
    &copy; 2019 <a href="">Ryan Wang's Blog</a>
    <br />
Server on Raspberry Pi
<br />
<a href="https://www.upyun.com/" target="_blank"><img src="/upload/2018/5/又拍云_logo5.png" style="width:48px"></img></a>
<script>
console.log("%c    __  __      __\n" +
                "   / / / /___ _/ /___\n" +
                "  / /_/ / __ `/ / __ \\\n" +
                " / __  / /_/ / / /_/ /\n" +
                "/_/ /_/\\__,_/_/\\____/ %c v1.1.1 https://github.com/halo-dev/halo","color:#4571ca;","color:red");
</script>
</div></div>

</div>
<script src="//cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js"
        integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="//cdn.jsdelivr.net/npm/popper.js@1.14.4/dist/umd/popper.min.js"
        integrity="sha256-EGs9T1xMHdvM1geM8jPpoo8EZ1V1VRsmcJz8OByENLA=" crossorigin="anonymous"></script>
<script src="//cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js"
        integrity="sha256-VsEqElsCHSGmnmHXGQzvoWjWwoznFSZc6hs7ARLRacQ=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"
        integrity="sha256-chlNFSVx3TdcQ2Xlw7SvnbLAavAQLO0Y/LBiWX04viY=" crossorigin="anonymous"></script>
<script src="//cdn.jsdelivr.net/npm/smooth-scroll@14.2.1/dist/smooth-scroll.polyfills.min.js"
        integrity="sha256-CI4Gq5E0io1Pv0xM3qPM+NUIOhbIBvC3GiN1Y4KhXpw=" crossorigin="anonymous"></script>
<script src="/halo-theme-Journal/source/js/journal.js"></script>

<!-- Import prettify js  -->
        <script src="/halo-theme-Journal/source/js/prettify.min.js"></script>
        <script>
            $(function() {
                $('pre').addClass('prettyprint linenums').attr('style', 'overflow:auto;');
                prettyPrint();
            })
        </script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"
        integrity="sha256-chlNFSVx3TdcQ2Xlw7SvnbLAavAQLO0Y/LBiWX04viY=" crossorigin="anonymous"></script>
<script src="http://localhost:8090/halo-comment.min.js?version=1.1.2"></script>
</body>
</html>